package com.wormhole.wormholemanagement.repository;

import com.wormhole.wormholemanagement.entity.PersonPosition;
import com.wormhole.wormholemanagement.entity.PersonPositionId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PersonPositionRepository extends JpaRepository<PersonPosition, PersonPositionId> {
    @Query("SELECT pp FROM PersonPosition pp JOIN FETCH pp.person p JOIN FETCH pp.position pos WHERE p.name LIKE %:name% OR pos.title LIKE %:title%")
    List<PersonPosition> search(@Param("name") String name, @Param("title") String title);

    // 修改为使用正确的属性路径
    @Query("SELECT pp FROM PersonPosition pp WHERE pp.person.personId = :personId")
    List<PersonPosition> findByPersonId(@Param("personId") Long personId);

    @Query("SELECT pp FROM PersonPosition pp WHERE pp.person.name = :personName")
    List<PersonPosition> findByPersonName(@Param("personName") String personName);

    @Modifying
    @Query("DELETE FROM PersonPosition pp WHERE pp.person.personId = :personId")
    void deleteByPersonId(@Param("personId") Long personId);
}